<style lang="less">
    @import './login.less';
</style>

<template>
    <div class="login" @keydown.enter="handleSubmit">
        <div class="login-con" :style="{left:Left,'margin-left':MarginLeft}">
            <Card :bordered="false">
                <p slot="title">
                    <Icon type="log-in"></Icon>
                    {{Title}}
                </p>
				<a href="#" slot="extra" @click.prevent="register">
					<Icon type="ios-loop-strong"></Icon>
					注册账号
				</a>
                <div class="form-con">
                    <Form ref="loginForm" :model="form" :rules="rules">
                        <FormItem prop="userName">
                            <Input v-model="form.userName" placeholder="请输入用户名">
                                <span slot="prepend">
                                    <Icon :size="16" type="person"></Icon>
                                </span>
                            </Input>
                        </FormItem>
                        <FormItem prop="password">
                            <Input type="password" v-model="form.password" placeholder="请输入密码">
                                <span slot="prepend">
                                    <Icon :size="14" type="locked"></Icon>
                                </span>
                            </Input>
                        </FormItem>
						<FormItem prop="repassword" v-if="isregister">
                            <Input type="password" v-model="form.repassword" placeholder="再次输入密码">
                                <span slot="prepend">
                                    <Icon :size="14" type="locked"></Icon>
                                </span>
                            </Input>
                        </FormItem>
                        <FormItem>
                            <Button @click="handleSubmit" type="primary" long>{{ btnText }}</Button>
                        </FormItem>
                    </Form>
                    <p class="login-tip"></p>
                </div>
            </Card>
        </div>
    </div>
</template>

<script>
import axios from 'axios'
export default {
    data () {
        return {
			isregister:false,
			showRegis:true,
            Title:'登录串口工具',
            btnText:'登录',
            login:true,
            Left:'auto',
            MarginLeft:'auto',
            form: {
                userName: '',
                password: '',
				repassword:''
            },
            rules: {
                userName: [
                    { required: true, message: '账号不能为空', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '密码不能为空', trigger: 'blur' }
                ]
            }
        };
    },
    components:{
        
    },
    created(){
        //检测usb插入
        let usbDetect = require('usb-detection');
        usbDetect.startMonitoring();
        usbDetect.on('add', function(device) {
            console.log(device);
        });
        usbDetect.on('change', function(device) { console.log('change', device); });
    },
    mounted(){
        
    },
    methods: {
        handleSubmit () {
            let vm = this;
            this.$refs.loginForm.validate((valid) => {
                if(valid){
                    if(this.login){
                        vm.$router.push('home_index');
						let userinfo = {uname:vm.form.userName,upwd:vm.form.password};
						vm.$store.commit('updateInfo',userinfo);
                    }else{
						if(vm.form.password !== vm.form.repassword){
							vm.$Message.error('两次输入密码不一致');
							return false;
						}
                        vm.$Message.success('注册成功');
                        this.login = true;
                        this.Title = '登录串口工具';
                        this.btnText = '登录';
                        this.showRegis = true;
                        this.isregister = false;
                    }
                }
            });


        },
        register:function(){
            this.login = false;
            this.Title = '注册串口工具';
            this.btnText = '注册';
			this.showRegis = false;
			this.isregister = true;
        }
    }
};
</script>

<style>

</style>
